package com.aidex.biz.controller;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.ShareLog;
import com.aidex.biz.service.ShareLogService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 分配记录Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/biz/bizShareLog")
public class ShareLogController extends BaseController
{
    @Autowired
    private ShareLogService shareLogService;

    /**
     * 查询分配记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:list')")
    @GetMapping("/list")
    public R<PageInfo> list(ShareLog shareLog, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                shareLog.setCompanyId(null);
        }else{
                     shareLog.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        shareLog.setPage(new PageDomain(request, response));
        return R.data(shareLogService.findPage(shareLog));
    }

    /**
     * 获取分配记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:query')")
    @GetMapping(value = "/{id}")
    public R<ShareLog> detail(@PathVariable("id") String id)
    {
        return R.data(shareLogService.get(id));
    }

    /**
     * 新增分配记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:add')")
    @Log(title = "分配记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  ShareLog shareLog)
    {
        return R.status(shareLogService.save(shareLog));
    }

    /**
     * 修改分配记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:edit')")
    @Log(title = "分配记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ShareLog shareLog)
    {
        return R.status(shareLogService.save(shareLog));
    }


    /**
     * 删除分配记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:remove')")
    @Log(title = "分配记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(shareLogService.deleteShareLogByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(shareLogService.findMaxSort(new ShareLog()));
    }

    /**
     * 导出分配记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShareLog:export')")
    @Log(title = "分配记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ShareLog shareLog)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    shareLog.setCompanyId(null);
            }else{
                         shareLog.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<ShareLog> list = shareLogService.findList(shareLog);
        ExcelUtil<ShareLog> util = new ExcelUtil<ShareLog>(ShareLog.class);
        return util.exportExcel(list, "分配记录数据");
    }

}
